package com.xxx.config.entity;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.xxx.config.api.AuditAware;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 自动生成固定字段数据策略
 *
 * @version 1.0
 * @since 1.0
 */
@Component
public class ColumnsHandler implements MetaObjectHandler {

    /**
     * 自动更新insert时候的字段
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updatedTime", LocalDateTime.class, LocalDateTime.now());

        this.strictInsertFill(metaObject, "createdBy", Long.class, Long.valueOf(AuditAware.getUserId()));
        this.strictUpdateFill(metaObject, "updatedBy", Long.class, Long.valueOf(AuditAware.getUserId()));
    }

    /**
     * 自动更新update时候的字段
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updatedTime", LocalDateTime.class, LocalDateTime.now());
        this.strictUpdateFill(metaObject, "updatedBy", Long.class, Long.valueOf(AuditAware.getUserId()));
    }
}
